package com.qfedu.web;

import com.qfedu.model.Category;
import com.qfedu.model.Product;
import com.qfedu.service.CategoryService;
import com.qfedu.service.ProductService;
import com.qfedu.service.impl.CategoryServiceImpl;
import com.qfedu.service.impl.ProductServiceImpl;
//import com.sun.org.apache.bcel.internal.generic.NEW;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;


//程序入口
public class ProductApp {

    public static void main(String[] args) {
        try {
            //键盘录入
            //Scanner scanner = new Scanner(System.in);
            //键盘录入缓冲流
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while (true){
                System.out.println("输入以下命令进行操作");
                System.out.println("C:创建 U:修改 D:删除 FA:查询所有 FI:根据id查询 DA:根据id批量删除 ");
                System.out.println("CC:创建商品分类 UC:修改商品分类 DC:删除商品分类 FAC:查询所有商品分类 FIC:根据id查询指定商品分类 Q:退出");
                String s = bufferedReader.readLine();
                switch (s.toUpperCase()) {
                    case "C":
                        createProduct(bufferedReader);
                        break;
                    case "U":
                        System.out.println("修改");
                        break;
                    case "D":
                        System.out.println("删除");
                        deleteProduct(bufferedReader);
                        break;
                    case "FA":
                        System.out.println("查询所有");
                        findAllProduct();
                        break;
                    case "FI":
                        System.out.println("根据id查询商品分类");
                        findProByPid(bufferedReader);
                        break;
                    case "DA":
                        System.out.println("根据id批量删除");
                        break;
                    case "Q":
                        System.out.println("退出系统");
                        System.exit(1);
                        break;
                    case "CC":
                        System.out.println("创建商品分类");
                        break;
                    case "UC":
                        System.out.println("修改商品分类");
                        break;
                    case "DC":
                        System.out.println("删除商品分类");
                        break;
                    case "FAC":
                        System.out.println("查询所有商品分类");
                        break;
                    case "FIC":
                        System.out.println("根据id查询商品分类信息");
                        
                        break;
                    default:
                        System.out.println("输入有误，请重试");
                        break;
                }
            }
        } catch (Exception e) {
            //模拟出现异常返回的友好页面
            System.out.println("系统正在维护，清稍后重试");
            e.printStackTrace();
        }

    }

//批量删除
    public static void deleteBatch(BufferedReader bufferedReader){
        ProductService productService=new ProductServiceImpl();
        CategoryService categoryService=new CategoryServiceImpl();
        System.out.println("请输入要删除的id，输入y代表结束：");
        //定义集合用来接收输入的id
        List list =new ArrayList<>();
        try {
            while(true){
                System.out.println("你好");
                //获取输入的数据
                String s =bufferedReader.readLine();
                if ("y".equals(s)) {
                    break;
                }else {
                    int pid = Integer.parseInt(s);
                    list.add(pid);
                }
            }
            productService.deleteBatch(list);
            System.out.println("批量删除成功");

        } catch (IOException e) {
            System.out.println("输入错误，请重新输入");
            return;

        }





    }



    //根据pid删除单个商品
    private  static  void deleteProduct(BufferedReader bufferedReader){
        //调用业务层
        ProductService productService=new ProductServiceImpl();
        CategoryService categoryService=new CategoryServiceImpl();

        try {
            System.out.println("请输入需要删除商品的pid:");
            String midPid=bufferedReader.readLine();
            int pid=Integer.parseInt(midPid);

            Product product= productService.findProByPid(pid);
            //System.out.println(1);
            if (product!=null){
                int row=productService.deleteProduct(pid);
                if (row>0)
                    System.out.println("删除成功");
                else
                    System.out.println("删除失败");
            }else {
                System.out.println("查不到此商品，请检查并重新输入");
                return;
            }
        } catch (IOException e) {
            System.out.println("输入有误，请重新输入");
            return;
        }


    }

    private static void createProduct(BufferedReader bufferedReader) {
        //调用业务层
        ProductService productService =  new ProductServiceImpl();
        CategoryService categoryService= new CategoryServiceImpl();
        try {
            System.out.println("请输入商品信息");
            System.out.println("输入商品的名称");
            String pname = bufferedReader.readLine();
            System.out.println("输入商品的价格");
            String sPrice = bufferedReader.readLine();
            int price = Integer.parseInt(sPrice);
            System.out.println("请选择商品是否上架：1 代表上架 0 代表下架");
            String s = bufferedReader.readLine();
            int flag = Integer.parseInt(s);
            //选择商品分类拓展需求
            //查询所有的商品分类
            List<Category> all = categoryService.findAll();
            // cname : cid
            Map<String,String> map = new HashMap<>();
            Map<String,String> map1 = new HashMap<>();
            // 手机：c001
            StringBuilder stringBuilder =  new StringBuilder();
            for (int i = 0; i < all.size(); i++) {
                map.put(all.get(i).getCname(),all.get(i).getCid());
                map1.put(i+1+"",all.get(i).getCname());
                stringBuilder.append(i+1).append("、").append(all.get(i).getCname()).append("\t");
            }

            System.out.println("请选择商品分类，输入编号");
            System.out.println(stringBuilder.toString());
            //用户输入对应的编号
            String s1 = bufferedReader.readLine();
            //获取编号对应的cname
            String s2 = map1.get(s1);
            //通过cname去获取对应的cid
            String cid = map.get(s2);
            Product product = new Product();
            product.setPname(pname);
            product.setFlag(flag);
            product.setPrice(price);
            product.setCid(cid);
            int row = productService.addProduct(product);
            if (row>0){
                System.out.println("添加成功");
            }

        } catch (IOException e) {
            System.out.println("输入信息有误，请重试");
            return;
        }

    }


    private static void findProByPid(BufferedReader bufferedReader) {
        //调用业务层
        ProductService productService =  new ProductServiceImpl();
        try {
            System.out.println("请输入要查询商品id");
            //获取id
            String pid = bufferedReader.readLine();
            //对输入的pid进行校验
            int id = Integer.parseInt(pid);
            //调用业务层
            Product product  =  productService.findProByPid(id);
            //System.out.println("查询到的商品信息："+product);   //
            System.out.println("商品id"+"\t"+"商品名称"+"\t"+"商品价格"+"\t"+"是否上架"+"\t"+"商品分类");
            System.out.println(product.getPid()+
                    "\t"+product.getPname()+"\t"+product.getPrice()+"\t"+(product.getFlag()==1?"上架":"下架")+"\t"+product.getCid());

        } catch (Exception e) {
            // e.printStackTrace();
            System.out.println("输入有误，请重新操作");
            return;
        }


    }

    //当前方法就是一个服务端接口
    //服务端作用:接收客户端的请求，然后调用业务层，返回结果到客户端
    private static void findAllProduct() {
        //调用业务层
        ProductService productService =  new ProductServiceImpl();

        List<Product> list = productService.findAll();
        //查询结果
        System.out.println("查询结果");

        for (Product product : list) {
            System.out.println(product);
        }

    }
}